package com.example.studentdemo.controller;

import com.example.studentdemo.dto.NotificationDTO;
import com.example.studentdemo.service.NotificationService;
import jakarta.annotation.Resource;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Map;

@Controller
public class NotificationController {

    @Resource
    private SimpMessagingTemplate messagingTemplate;

    @Resource
    private NotificationService notificationService;

    /**
     * 处理发送通知的请求
     * 前端通过STOMP客户端发送到/app/sendNotification
     */
    @MessageMapping("/sendNotification")
    public void sendNotification(@Payload NotificationDTO notificationDTO, Principal principal) {
        // 保存通知到数据库
        notificationService.saveNotification(notificationDTO);

        // 根据接收者类型发送通知
        switch (notificationDTO.getRecipientType()) {
            case "ALL":
                // 发送给所有用户
                messagingTemplate.convertAndSend("/topic/notifications", notificationDTO);
                break;
            case "TEACHER":
                // 发送给教师
                messagingTemplate.convertAndSend("/topic/notifications/teachers", notificationDTO);
                break;
            case "STUDENT":
                // 发送给学生
                messagingTemplate.convertAndSend("/topic/notifications/students", notificationDTO);
                break;
            case "ADMIN":
                // 发送给管理员
                messagingTemplate.convertAndSend("/topic/notifications/admins", notificationDTO);
                break;
            default:
                // 默认发送给指定用户
                if (notificationDTO.getRecipientId() != null) {
                    messagingTemplate.convertAndSendToUser(
                        String.valueOf(notificationDTO.getRecipientId()),
                        "/queue/notifications",
                        notificationDTO
                    );
                }
                break;
        }
    }

    /**
     * REST API端点，用于发送通知
     */
    @PostMapping("/api/notifications")
    @ResponseBody
    public Map<String, Object> sendNotificationViaRest(@RequestBody NotificationDTO notificationDTO) {
        try {
            // 保存通知到数据库
            notificationService.saveNotification(notificationDTO);

            // 根据接收者类型发送通知
            switch (notificationDTO.getRecipientType()) {
                case "ALL":
                    // 发送给所有用户
                    messagingTemplate.convertAndSend("/topic/notifications", notificationDTO);
                    break;
                case "TEACHER":
                    // 发送给教师
                    messagingTemplate.convertAndSend("/topic/notifications/teachers", notificationDTO);
                    break;
                case "STUDENT":
                    // 发送给学生
                    messagingTemplate.convertAndSend("/topic/notifications/students", notificationDTO);
                    break;
                case "ADMIN":
                    // 发送给管理员
                    messagingTemplate.convertAndSend("/topic/notifications/admins", notificationDTO);
                    break;
                default:
                    // 默认发送给指定用户
                    if (notificationDTO.getRecipientId() != null) {
                        messagingTemplate.convertAndSendToUser(
                            String.valueOf(notificationDTO.getRecipientId()),
                            "/queue/notifications",
                            notificationDTO
                        );
                    }
                    break;
            }

            return Map.of("success", true, "message", "通知发送成功");
        } catch (Exception e) {
            return Map.of("success", false, "message", "通知发送失败: " + e.getMessage());
        }
    }
}